<?php

use Core\API_Controller;
use Libraries\Uploader;
class UploadController extends API_Controller{

	public function index(){
		$file = Uploader::image('user_file');

		if ($file) {
			return $this->success([
				'path' =>'/uploadfiles/image/'. $file,
				'url' => '/uploadfiles/image/'.$file,
			]);
		} else {
			$this->error('上传失败');
		}
		
		$this->success($data);
	}

	/**
	 * 图片上传
	 * @param file 图片表单名
	 */
	public function image(){
		$img = Uploader::image();
		if($img){
			echo json_encode(array('code'=>0,'msg'=>'ok','data'=>array('img'=>'/uploadfiles/image/'.$img)));
	        
		} else {
			echo json_encode(array('code'=>1,'msg'=>'上传失败'));
		}
	}

	/**
	 * 文件上传
	 * @param file 图片表单名
	 */
	public function file(){
		$file = Uploader::file();
		if($file){
			//阿里云oss上传图片备份
	        $res = oss('avatar/'.$file , '../static/files/'.$file);
	        if(!$res){
	            unlink('../static/files/'.$file);
	            $this->show_json(array(),40001,'上传失败oss');
	        }
			$this->show_json(array('file'=>$file));
		} else {
			$this->show_json(NULL,50001);	
		}
		
	}

	/**
	 * Ueditor 上传
	 */
	public function ueditor(){
		$this->output->set_header('Access-Control-Allow-Origin:*');
		$CONFIG = json_decode(preg_replace("/\/\*[\s\S]+?\*\//", "", file_get_contents("http://assets.cjkt.com/ueditor/php/config.json")), true);
		$action = $this->input->get('action');
		$result =  array(
			"state" => "SUCCESS",          //上传状态，上传成功时必须返回"SUCCESS"
			"url" => "",            //返回的地址
			"title" => "",          //新文件名
			"original" => "",       //原始文件名
			"type" => "" ,           //文件类型
			"size" => "",           //文件大小
		);
		switch($action){
			case 'config':
	        	$result = json_encode($CONFIG);
	        	break;
			/* 上传图片 */
		    case 'uploadimage':
		    	$img = Uploader::image('upfile');
		    	//阿里云oss上传图片备份
		        $res = oss('uploads/'.$img , '../static/uploads/'.$img);
		        if(!$res){
		            unlink('../static/uploads/'.$img);
		            $this->show_json(array(),40001,'上传失败oss');
		        }

		    	$result['url'] = IMG_URL.$img;
		    	$result['title'] = substr($img, strrpos($img, '/') + 1);
		    	$result['original'] = $_FILES['upfile']['name'];
		    	$result['name'] = substr($img, strrpos($img, '/') + 1);
		    	$result['type'] = strtolower(strrchr($img, '.'));
		    	$result['size'] = strval(filesize('../static/uploads/'.$img));
		    	$result =  json_encode($result);
		    	break;
		    /* 上传涂鸦 */
		    case 'uploadscrawl':
				    // $config = array(
			     //        "pathFormat" => $CONFIG['scrawlPathFormat'],
			     //        "maxSize" => $CONFIG['scrawlMaxSize'],
			     //        "allowFiles" => $CONFIG['scrawlAllowFiles'],
			     //        "oriName" => "scrawl.png"
			     //    );
		        $fieldName = $CONFIG['scrawlFieldName'];
		        $base64 = "base64";
		        $img = Uploader::base64_up($_POST[$fieldName],'../static/uploads','png');
		        //阿里云oss上传图片备份
		        $res = oss('uploads/'.$img , '../static/uploads/'.$img);
		        if(!$res){
		            unlink('../static/uploads/'.$img);
		            $this->show_json(array(),40001,'上传失败oss');
		        }

		    	$result['url'] = IMG_URL.$img;
		    	$result['title'] = substr($img, strrpos($img, '/') + 1);
		    	$result['original'] = 'sca.png';
		    	$result['name'] = substr($img, strrpos($img, '/') + 1);
		    	$result['type'] = strtolower(strrchr($img, '.'));
		    	$result['size'] = strval(filesize('../static/uploads/'.$img));
		    	$result =  json_encode($result);
		    	break;
		    /* 上传视频 */
		    case 'uploadvideo':
		    /* 上传文件 */
		    case 'uploadfile':

		        //$result = include("action_upload.php");
		        break;

		    /* 列出图片 */
		    case 'listimage':
		        //$result = include("action_list.php");
		        break;
		    /* 列出文件 */
		    case 'listfile':
		        //$result = include("action_list.php");
		        break;

		    /* 抓取远程文件 */
		    case 'catchimage':
		        //$result = include("action_crawler.php");
		        break;
		}
		/* 输出结果 */
		if (isset($_GET["callback"])) {
		    if (preg_match("/^[\w_]+$/", $_GET["callback"])) {
		        echo htmlspecialchars($_GET["callback"]) . '(' . $result . ')';
		    } else {
		        echo json_encode(array(
		            'state'=> 'callback参数不合法'
		        ));
		        echo 'alert("callback参数不合法")';
		        exit;
		    }
		} else {
		    echo $result;
		}
	}
}
